home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / rfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-02  |  3.8 KB  |  123 lines

  1. /*
  2.  
  3.     File: rfs.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <string.h> 
  25. #include "types.h"
  26. #include "common.h"
  27. #include "rfs.h"
  28. #include "fnctdsk.h"
  29. #include "intrface.h"
  30. static int set_rfs_info(const t_param_disk *disk_car, const struct reiserfs_super_block *sb,t_diskext *partition, const int debug, const int dump_ind);
  31. static int test_rfs(const t_param_disk *disk_car, const struct reiserfs_super_block *sb,t_diskext *partition,const int debug, const int dump_ind);
  32.  
  33. int check_rfs(t_param_disk *disk_car,t_diskext *partition,const int debug)
  34. {
  35.   unsigned char buffer[8*SECTOR_SIZE];
  36.   if(disk_car->read(disk_car,8, &buffer, partition->lba+128)!=0) /* 64k offset */
  37.   { return 1; }
  38.   if(test_rfs(disk_car,(struct reiserfs_super_block*)&buffer,partition,debug,0)!=0)
  39.     return 1;
  40.   set_rfs_info(disk_car,(struct reiserfs_super_block*)&buffer,partition,debug,0);
  41.   return 0;
  42. }
  43.  
  44. static int test_rfs(const t_param_disk *disk_car, const struct reiserfs_super_block *sb,t_diskext *partition,const int debug, const int dump_ind)
  45. {
  46.   if (memcmp(sb->s_magic,REISERFS_SUPER_MAGIC,sizeof(REISERFS_SUPER_MAGIC)) == 0)
  47.   {
  48.     partition->upart_type = UP_RFS;
  49.   }
  50.   else
  51.     if(memcmp(sb->s_magic,REISERFS2_SUPER_MAGIC,sizeof(REISERFS2_SUPER_MAGIC)) == 0)
  52.     {
  53.       partition->upart_type = UP_RFS2;
  54.     }
  55.     else
  56.       return 1;
  57.   if(debug!=0)
  58.     ecrit_rapport("\nReiserFS Marker at %u/%u/%u\n", LBA2cylinder(disk_car,partition->lba),LBA2head(disk_car,partition->lba),LBA2sector(disk_car,partition->lba));
  59.   /*
  60.    * sanity checks.
  61.    */
  62.  
  63.   if (sb->s_block_count < sb->s_free_blocks)
  64.     return (1);
  65.  
  66.   if (sb->s_block_count < REISERFS_MIN_BLOCK_AMOUNT)
  67.     return (1);
  68.  
  69.   if ((sb->s_state != REISERFS_VALID_FS) &&
  70.       (sb->s_state != REISERFS_ERROR_FS))
  71.     return (1);
  72.  
  73.   if (sb->s_oid_maxsize % 2!=0) /* must be even */
  74.     return (1);
  75.  
  76.   if (sb->s_oid_maxsize < sb->s_oid_cursize)
  77.     return (1);
  78.  
  79.   if ((sb->s_blocksize != 4096) && (sb->s_blocksize != 8192))
  80.     return (1);
  81.  
  82.   return 0;
  83. }
  84.  
  85. int recover_rfs(t_param_disk *disk_car, const struct reiserfs_super_block *sb,t_diskext *partition,const int debug, const int dump_ind)
  86. {
  87.   if(test_rfs(disk_car,sb,partition,debug,dump_ind)!=0)
  88.     return 1;
  89.   if(debug!=0 || dump_ind!=0)
  90.   {
  91.     ecrit_rapport("\nrecover_rfs\n");
  92.     ecrit_rapport("block_count=%u\n",sb->s_block_count);
  93.     ecrit_rapport("block_size=%u\n",sb->s_blocksize);
  94.     if(dump_ind!=0)
  95.     {
  96.       dump(stdscr,sb,SECTOR_SIZE);
  97.     }
  98.   }
  99.  
  100.   partition->part_size = sb->s_block_count * (sb->s_blocksize / SECTOR_SIZE);
  101.   partition->part_type = (unsigned char)0x83;
  102.   set_rfs_info(disk_car,sb,partition,debug,dump_ind);
  103.   return 0;
  104. }
  105.  
  106. static int set_rfs_info(const t_param_disk *disk_car,const struct reiserfs_super_block *sb,t_diskext *partition, const int debug, const int dump_ind)
  107. {
  108.   partition->info[0]='\0';
  109.   switch(partition->upart_type)
  110.   {
  111.     case UP_RFS:
  112.       strncpy(partition->info,"ReiserFS 1",sizeof(partition->info));
  113.       break;
  114.     case UP_RFS2:
  115.       strncpy(partition->info,"ReiserFS 2",sizeof(partition->info));
  116.       set_part_name(partition,sb->s_label,16);
  117.       break;
  118.     default:
  119.       return 1;
  120.   }
  121.   return 0;
  122. }
  123.